package jmotor.util;

import java.util.StringTokenizer;

/**
 * Component:
 * Description:
 * Date: 11-8-10
 *
 * @author Andy.Ai
 */
public class StringUtils {
    private StringUtils() {
    }

    public static boolean isBlank(String str) {
        return isNull(str) || str.trim().length() == 0;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isNull(String str) {
        return null == str;
    }

    public static boolean isNotNull(String str) {
        return !isNull(str);
    }

    public static String replaceNull(String str) {
        return replaceNull(str, "");
    }

    public static String replaceNull(String str, String replacement) {
        if (str == null) {
            return replacement;
        }
        return str;
    }

    public static String replace(String str, String target, String replacement) {
        int startIndex = 0;
        int endIndex = str.indexOf(target);
        String result = "";
        if (endIndex != -1) {
            while (endIndex != -1) {
                result += str.substring(startIndex, endIndex) + replacement;
                startIndex = endIndex + target.length();
                endIndex = str.indexOf(target, startIndex);
            }
            result += str.substring(startIndex);
        } else {
            return str;
        }
        return result;
    }

    public static String[] split(String str, String separator) {
        StringTokenizer tokenizer = new StringTokenizer(str, separator);
        String[] result = new String[tokenizer.countTokens()];
        int index = 0;
        while (tokenizer.hasMoreTokens()) {
            result[index++] = tokenizer.nextToken();
        }
        return result;
    }

    public static String firstCharter(String str) {
        return String.valueOf(str.charAt(0));
    }

    public static String lastCharter(String str) {
        return String.valueOf(str.charAt(str.length() - 1));
    }
}
